import React, { useRef, useEffect } from 'react';
import * as echarts from 'echarts';
import moment from 'moment';
import styles from './index.less';

type EChartsOption = echarts.EChartsOption;

export default function Line(prors: any) {
  const { items } = prors;
  let option: EChartsOption;
  const lineRef = useRef<HTMLDivElement>(null);
  let myChart: any;
  const initChart = () => {
    if (myChart != null && myChart != undefined) {
      myChart.dispose();
    }
    let x = items.xtimes?.map((item: any) => moment(item).format('HH:mm'));
    let y = items.ydatas?.map((item: any) => item.value);
    option = {
      xAxis: {
        type: 'category',
        data: x,
        axisLine: {
          lineStyle: {
            width: 1,
            color: '#7EA7BA',
            type: 'dotted',
          },
        },
        // data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
      },
      yAxis: {
        type: 'value',
        axisLine: {
          lineStyle: {
            width: 0.01,
            color: '#9BB7EF',
          },
        },
      },
      tooltip: {
        trigger: 'axis',
        padding: 5,
        confine: true,
        axisPointer: {
          type: 'shadow',
        },
      },
      grid: {
        left: '3%',
        right: '1%',
        bottom: '5%',
        top: '5%',
        containLabel: true,
      },
      series: [
        {
          // data: [150, 230, 224, 218, 135, 147, 260],
          data: y,
          type: 'line',
        },
      ],
    };
    myChart = echarts.init(lineRef.current!);

    option && myChart.setOption(option);
  };
  useEffect(() => {
    if (items?.xtimes?.length > 0) {
      initChart();
    }
  }, [items]);
  return <div ref={lineRef} className={`${styles.lineBox}`}></div>;
}
